package his.controller;


import com.alibaba.fastjson.JSONObject;
import his.entity.RegistLevel;
import his.entity.User;
import his.persistence.RegistLevelMapper;
import his.service.RegistLevelService;
import org.example.his.common.api.CommonResponse;
import org.example.his.common.utils.RequestToJsonObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/admin/reg/")
public class RegistLevelController {

    @Autowired
    RegistLevelService registLevelService;

    @GetMapping("all")
    @ResponseBody
    public CommonResponse<List<RegistLevel>> getall_registlevels()
    {
        return  registLevelService.getall_registlevels();
    }

    @PostMapping("add")
    @ResponseBody
    public CommonResponse<List<RegistLevel>> add_registlevels(HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String registCode=(String) jsonObject.get("registCode");
        String registName=(String) jsonObject.get("registName");
        int sequenceNo=Integer.parseInt((String) jsonObject.get("sequenceNo")) ;
        BigDecimal registFee= new BigDecimal((String) jsonObject.get("registFee")) ;
        int registQuota=Integer.parseInt((String) jsonObject.get("registQuota")) ;
        return  registLevelService.add_registlevels(registCode,registName,sequenceNo,registFee,registQuota);
    }

    @PostMapping("modify/{sequence_no}")
    @ResponseBody
    public CommonResponse<List<RegistLevel>> update_registlevels(@PathVariable int sequence_no,HttpServletRequest httpServletRequest)
    {
        JSONObject jsonObject= RequestToJsonObject.RequestToJsonObject(httpServletRequest);
        String registCode=(String) jsonObject.get("registCode");
        String registName=(String) jsonObject.get("registName");
        int sequenceNo=(int) jsonObject.get("sequenceNo");
        BigDecimal registFee= (BigDecimal) jsonObject.get("registFee");
        int registQuota=Integer.parseInt((String) jsonObject.get("registQuota"));
        return  registLevelService.update_registlevels(sequence_no,registCode,registName,sequenceNo,registFee,registQuota);
    }

    @DeleteMapping ("del/{sequence_no}")
    @ResponseBody
    public CommonResponse<List<RegistLevel>> delete_registlevels(@PathVariable int sequence_no)
    {
        return  registLevelService.delete_registlevels(sequence_no);
    }

    @GetMapping ("search/{keyword}")
    @ResponseBody
    public CommonResponse<List<RegistLevel>> getall_registlevels_bycodename(@PathVariable String keyword)
    {
        return  registLevelService.getall_registlevels_bycodename(keyword);
    }
}
